package rsj.api.customer;

import com.rlyy.aibase.utils.base.Box;
import common.checkgroup.AddGroup;
import common.checkgroup.UpdateGroup;
import common.util.Assert;
import common.util.BoxUtil;
import common.util.validator.ParameterConversionCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.customer.entity.Customer;
import rsj.api.customer.entity.CustomerQueryParam;
import rsj.log.ModuleEnum;
import rsj.log.OperationLog;
import rsj.log.OperationTypeEnum;
import rsj.service.customer.ICustomerService;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 客户
 *
 * @author yuanqinglong
 * @since 2021/7/14 11:21
 */
@RestController
public class CustomerController {


    @Autowired
    private ICustomerService customerServiceImpl;


    /**
     * 客户列表
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_customer_list")
    public Box customerList(Box param) {
        CustomerQueryParam customerQueryParam = ParameterConversionCheck.parameterConversionCheckFastFail(param, CustomerQueryParam.class);
        List<Customer> customers = customerServiceImpl.customerList(customerQueryParam);
        int count = customerServiceImpl.customerCount(customerQueryParam);
        return BoxUtil.successBoxToTableData(param, customers, count);
    }


    /**
     * 添加客户
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_customer_add")
    @OperationLog(description = "新增客户", logType = OperationTypeEnum.ADD, logModule = ModuleEnum.CUSTOMER)
    public Box addCustomer(Box param) {
        Customer customer = ParameterConversionCheck.parameterConversionCheckFastFail(param, Customer.class, AddGroup.class, Default.class);
        customerServiceImpl.addCustomer(customer);
        return BoxUtil.successBox(param);
    }


    /**
     * 更新客户
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_customer_update")
    @OperationLog(description = "修改客户", logType = OperationTypeEnum.UPDATE, logModule = ModuleEnum.CUSTOMER)
    public Box updateCustomer(Box param) {
        Customer customer = ParameterConversionCheck.parameterConversionCheckFastFail(param, Customer.class, UpdateGroup.class);
        customerServiceImpl.updateCustomer(customer);
        return BoxUtil.successBox(param);
    }


    /**
     * 得到客户
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_customer_get")
    public Box getCustomer(Box param) {
        String customerId = param.getMapNode("data").getValue("cust_id");
        Assert.isNotBlank(customerId, "客户id不可为空");
        Customer customer = customerServiceImpl.getCustomer(customerId);
        return BoxUtil.successBoxToData(param, customer);
    }


    /**
     * 选择客户列表
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_customer_select_list")
    public Box selectCustomerList(Box param) {
        CustomerQueryParam customerQueryParam = ParameterConversionCheck.parameterConversionCheckFastFail(param, CustomerQueryParam.class);
        List<Customer> customers = customerServiceImpl.selectCustomerList(customerQueryParam);
        //int count = customerServiceImpl.customerCount(customerQueryParam);
        return BoxUtil.successBoxToTableData(param, customers);
    }
}
